﻿var common = function ($) {
    var config = {
        apiHost: 'http://localhost:9999'
    };

    /*
     * from relative url build full url
     */
    var buildApiUrl = function (relativeUrl) {
        if (relativeUrl.substring(0, 1) != '/') {
            relativeUrl = '/' + relativeUrl;
        }
        return config.apiHost + relativeUrl;
    }

    /*
     * send post request
     */
    var post = function (url, data, callback, options) {
        var dataType = options ? options.dataType : 'json';

        var settings = {
            type: "POST",
            url: url,
            data: JSON.stringify(data),
            dataType: dataType,
            contentType: "application/json; charset=utf-8",
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr + "\n" + textStatus + "\n" + errorThrown);
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', window._token);
            },
            success: function (data, textStatus) {
                if (data && data.status == "200") {
                    if (callback) {
                        callback(data.result);
                    }
                }
                else if (data != null) {
                    alert(data.errMsg);
                }
                else {
                    alert("请求失败！");
                }
            }
        };

        $.ajax(settings);
    }

    /*
     * send get request
     */
    var get = function (url, data, callback, options) {
        var settings = {
            type: "GET",
            url: url,
            data: data,
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr + "\n" + textStatus + "\n" + errorThrown);
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', window._token);
            },
            success: function (data, textStatus) {
                if (data && data.status == "200") {
                    if (callback) {
                        callback(data.result);
                    }
                }
                else if (data != null) {
                    alert(data.errMsg);
                }
                else {
                    alert("请求失败！");
                }
            }
        };

        $.ajax(settings);
    }

    /*
     * 检查token是否存在，如果不存在则跳到登录页面
     */
    var checkToken = function () {
        if (!window._token || window._token.length == 0) {
            window.location.href = "/user/login";
        }
    }

    /*
     * Get the value of a querystring
     * @param  {String} field The field to get the value of
     * @param  {String} url   The URL to get the value from (optional)
     * @return {String}       The field value
     */
    var getQueryString = function (field, url) {
        var href = url ? url : window.location.href;
        var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
        var string = reg.exec(href);
        return string ? string[1] : null;
    };

    return {
        buildApiUrl: buildApiUrl,
        config: config,
        post: post,
        get: get,
        checkToken: checkToken,
        getQueryString: getQueryString
    }

}($);